<html>
	<head>
		<script runat="server" src="../../src/Norm.js"></script>
		<script runat="server" src="../../src/Norm.Logger.Jaxer.js"></script>
		<script runat="server" src="../../src/Norm.Database.Jaxer.js"></script>
		<script runat="server" src="../../src/Norm.ResultIterator.Jaxer.js"></script>
		<script runat="server" src="../../src/Norm.LastIdManager.js"></script>
		<script runat="server" src="../../src/Norm.Events.js"></script>
		<script runat="server" src="../../src/Norm.Sql.js"></script>
		<script runat="server" src="../../src/Norm.Fields.js"></script>
		<script runat="server" src="../../src/Norm.Model.js"></script>
		<script runat="server" src="../../src/Norm.Introspection.js"></script>
		<script runat="server" src="../../src/Norm.Transaction.js"></script>
		<script runat="server">
			Norm.Database.name = "test.selfRelation";
			var TreeNode = new Norm.Model({
				name : "TreeNode",
				fields : {
					data : new Norm.Fields.String({maxLength : 25}),
					parent : new Norm.Fields.OneToMany({related : "TreeNode", allowNull : true}),
					children : new Norm.Fields.ManyToOne({related : "TreeNode"})
				}
			});
			
			var Tests = {
				
				up : function() {
					
					Norm.LastIdManager.resetTable("TreeNode");			// this test may expect reset row ids after dropping the table
					TreeNode.dropTable();
					TreeNode.createTable();
				},
				
				"test that create with null and inner relation works" : function() {
					
					var root = new TreeNode({data : "i am the root", parent : null}).save();
					var node1 = new TreeNode({data : "i am node 1", parent : root}).save();
					var node2 = new TreeNode({data : "i am node 2", parent : root}).save();
				},
				
				"test that select works" : function() {
					UnitTest.assertEqual(2, new TreeNode(1).children.select().toArray().length);
				},
				
				"test that remove works" : function() {
					var root = new TreeNode(1);
					root.children.remove();
					UnitTest.assertEqual(0, root.children.select().toArray().length);
				},
				
				down : function() {
					TreeNode.dropTable();
				}
			};
		</script>
		<script runat="server" src="unit-test.js"></script>
	</head>
	<body>
		
	</body>
</html>
